package com.lk.security.authorize;

import com.lk.security.properties.AuthenticationProperties;
import com.lk.security.properties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.stereotype.Component;

/**
 * @program: lk-security-parent
 * @description: 身份认证相关请求直接放行
 * @author: Aspirin
 * @create: 2020-06-14 23:18
 * @remarks Order(Integer.MAX_VALUE) // 值越小加载越优先，值越大加载越靠后
 */
@Component
@Order(Integer.MAX_VALUE)
public class CustomAuthorizeConfigurerProvider implements AuthorizeConfigurerProvider {
  @Autowired private SecurityProperties securityProperties;

  @Override
  public void configure(
      ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry config) {
    AuthenticationProperties authentication = securityProperties.getAuthentication();
    config
        .antMatchers(
            // "/code/image","/mobile/page", "/code/mobile"
            authentication.getLoginPage(),
            authentication.getImageCodeUrl(),
            authentication.getMobilePage(),
            authentication.getMobileCodeUrl())
        .permitAll();

    // 其他请求都要通过身份认证
    config.anyRequest().authenticated();
  }
}
